package com.ruoyi.report.service.impl;

import java.math.BigDecimal;
import java.util.*;

import com.hig.ledger.mapper.LedgerSpecialVehiclesMapper;
import com.hig.risk.mapper.RiskHazardInvestigationMapper;
import com.hig.risk.mapper.RiskScoreControlMapper;
import com.hig.risk.mapper.RiskSelfInspectionMapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.onway.domain.ReportOnway;
import com.ruoyi.onway.mapper.ReportOnwayMapper;
import com.ruoyi.report.domain.*;
import com.ruoyi.report.dto.DataObj;
import com.ruoyi.report.dto.FileGroupDTO;
import com.ruoyi.report.dto.ReportDataDTO;
import com.ruoyi.report.mapper.ReportPlanDetailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.report.mapper.ReportPlanMapper;
import com.ruoyi.report.service.IReportPlanService;

/**
 * 采购计划Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-12
 */
@Service
public class ReportPlanServiceImpl implements IReportPlanService 
{
    @Autowired
    private ReportPlanMapper reportPlanMapper;
    @Autowired
    private ReportPlanDetailMapper reportPlanDetailMapper;
    @Autowired
    private ReportOnwayMapper reportOnwayMapper;
    @Autowired
    private RiskSelfInspectionMapper riskSelfInspectionMapper;
    @Autowired
    private RiskHazardInvestigationMapper riskHazardInvestigationMapper;
    @Autowired
    private RiskScoreControlMapper riskScoreControlMapper;
    @Autowired
    private LedgerSpecialVehiclesMapper ledgerSpecialVehiclesMapper;
    /**
     * 查询采购计划
     * 
     * @param id 采购计划ID
     * @return 采购计划
     */
    @Override
    public ReportPlan selectReportPlanById(Long id)
    {
        return reportPlanMapper.selectReportPlanById(id);
    }

    /**
     * 查询采购计划列表
     * 
     * @param reportPlan 采购计划
     * @return 采购计划
     */
    @Override
    public List<ReportPlan> selectReportPlanList(ReportPlan reportPlan)
    {
        return reportPlanMapper.selectReportPlanList(reportPlan);
    }

    /**
     * 新增采购计划
     * 
     * @param reportPlan 采购计划
     * @return 结果
     */
    @Transactional
    @Override
    public int insertReportPlan(ReportPlan reportPlan)
    {
        reportPlan.setCreateTime(DateUtils.getNowDate());
        int rows = reportPlanMapper.insertReportPlan(reportPlan);
        insertReportPlanDetail(reportPlan);
        return rows;
    }

    @Override
    public int countCurrentMonthPlans() {
       return reportPlanMapper.countCurrentMonthPlans();
    }

    /**
     * 修改采购计划
     * 
     * @param reportPlan 采购计划
     * @return 结果
     */
    @Transactional
    @Override
    public int updateReportPlan(ReportPlan reportPlan)
    {
        reportPlan.setUpdateTime(DateUtils.getNowDate());
        reportPlanMapper.deleteReportPlanDetailByPlanId(reportPlan.getId());
        insertReportPlanDetail(reportPlan);
        return reportPlanMapper.updateReportPlan(reportPlan);
    }

    /**
     * 批量删除采购计划
     * 
     * @param ids 需要删除的采购计划ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteReportPlanByIds(Long[] ids)
    {
        reportPlanMapper.deleteReportPlanDetailByPlanIds(ids);
        return reportPlanMapper.deleteReportPlanByIds(ids);
    }

    /**
     * 删除采购计划信息
     * 
     * @param id 采购计划ID
     * @return 结果
     */
    @Override
    public int deleteReportPlanById(Long id)
    {
        reportPlanMapper.deleteReportPlanDetailByPlanId(id);
        return reportPlanMapper.deleteReportPlanById(id);
    }

    @Override
    public DaysDTO queryLasa7days() {
        return reportPlanMapper.queryLasa7days();
    }

    @Override
    public DaysDTO queryAli7days() {
        return reportPlanMapper.queryAli7days();
    }

    @Override
    public DaysDTO queryLinzhi7days() {
        return reportPlanMapper.queryLinzhi7days();
    }

    @Override
    public DaysDTO queryRikaze7days() {
        return reportPlanMapper.queryRikaze7days();
    }

    @Override
    public List<ExamScoreStatistics> getExamScoreStatistics() {
        return reportPlanMapper.getExamScoreStatistics();
    }

    @Override
    public List<RefineryAllocation> getRefineryAllocations() {
        return reportPlanMapper.getRefineryAllocations();
    }

    @Override
    public DaysDTO queryPeisong7days() {
        return reportPlanMapper.queryPeisong7days();
    }

    @Override
    public List<ReportPlanInfo> selectReportPlanListInfo(ReportPlan reportPlan) {
        List<ReportPlanInfo> list = reportPlanMapper.selectReportPlanInfoList(reportPlan);
        for (ReportPlanInfo plan : list){
            List<ReportPlanDetailInfo> detailInfos = reportPlanDetailMapper.selectDetailInfoList(plan.getId());
            for (ReportPlanDetailInfo detail : detailInfos){
                List<ReportOnway> onways = reportOnwayMapper.selectOnwayList(plan.getId(),detail.getRefineryId());
                detail.setOnways(onways);
            }
            plan.setDetailInfos(detailInfos);
        }
        return list;
    }

    @Override
    public ReportDataDTO queryLasaDaily(String date) {
        return reportPlanMapper.queryLasaDaily(date);
    }

    @Override
    public ReportDataDTO queryLinzhiDaily(String date) {
        return reportPlanMapper.queryLinzhiDaily(date);
    }

    @Override
    public ReportDataDTO queryRikazeDaily(String date) {
        return reportPlanMapper.queryRikazeDaily(date);
    }

    @Override
    public ReportDataDTO queryAliDaily(String date) {
        return reportPlanMapper.queryAliDaily(date);
    }

    @Override
    public ReportDataDTO queryPeisongzhongxinDaily(String date) {
        return reportPlanMapper.queryPeisongzhongxinDaily(date);
    }

    @Override
    public DataObj screen(DataObj dataObj) {
        //查询当前月重大隐患
        DataObj.MajorHazardInspection majorHazardInspection = new DataObj.MajorHazardInspection();
        majorHazardInspection.setTableData(riskSelfInspectionMapper.selectForScreen());
        dataObj.setMajorHazardInspection(majorHazardInspection);
        //查询当前月一般隐患
        DataObj.GeneralHazardInspection generalHazardInspection = new DataObj.GeneralHazardInspection();
        generalHazardInspection.setTableData(riskHazardInvestigationMapper.selectForScreen());
        dataObj.setGeneralHazardInspection(generalHazardInspection);
        //查询当前月风险分级
        DataObj.RiskLevelManagement riskLevelManagement = new DataObj.RiskLevelManagement();
        riskLevelManagement.setTableData(riskScoreControlMapper.selectForScreen());
        dataObj.setRiskLevelManagement(riskLevelManagement);
        //查询设备设施
        DataObj.EquipmentFacilityManagement equipmentFacilityManagement = new DataObj.EquipmentFacilityManagement();
        equipmentFacilityManagement.setTableData(ledgerSpecialVehiclesMapper.selectForScreen());
        dataObj.setEquipmentFacilityManagement(equipmentFacilityManagement);
        return dataObj;
    }
    @Override
    public List<FileGroupDTO> selectFileGroup() {
        return reportPlanMapper.selectFileGroup();
    }

    /**
     * 新增${subTable.functionName}信息
     * 
     * @param reportPlan 采购计划对象
     */
    public void insertReportPlanDetail(ReportPlan reportPlan)
    {
        List<ReportPlanDetail> reportPlanDetailList = reportPlan.getReportPlanDetailList();
        Long id = reportPlan.getId();
        if (StringUtils.isNotNull(reportPlanDetailList))
        {
            List<ReportPlanDetail> list = new ArrayList<ReportPlanDetail>();
            for (ReportPlanDetail reportPlanDetail : reportPlanDetailList)
            {
                reportPlanDetail.setPlanId(id);
                list.add(reportPlanDetail);
            }
            if (list.size() > 0)
            {
                reportPlanMapper.batchReportPlanDetail(list);
            }
        }
    }
}
